package com.ygsj.app;

import android.content.Context;

import java.util.HashMap;

import static com.ygsj.app.ConfigKeys.HANDLER;

/*全局变量 自己用,所以没有check*/
public class Configurator {
    private static final HashMap<Object, Object> CONFIGS = new HashMap<>();

    /*---------单例--------*/
    private Configurator() {
       // CONFIGS.put(ConfigKeys.CONFIG_READY, false);//初始化化的时候置为false;
       // CONFIGS.put(ConfigKeys.HANDLER, HANDLER);
    }

   public static Configurator getInstance() {
        return Holder.INSTANCE;
    }

    private static class Holder {
        private static final Configurator INSTANCE = new Configurator();
    }
    /*---------单例结束--------*/


    public final Configurator withAppContext(Context context) {
        CONFIGS.put(ConfigKeys.APPLICATION_CONTEXT, context.getApplicationContext());
        return this;
    }

//    public final Configurator withApiHost(String host) {
//        CONFIGS.put(ConfigKeys.API_HOST, host);
//        return this;
//    }

    public final Configurator withLoaderDelayed(long delayed) {
       CONFIGS.put(ConfigKeys.LOADER_DELAYED, delayed);
        return this;
    }

   /* private void checkConfiguration() {
        final boolean isReady = (boolean) CONFIGS.get(ConfigKeys.CONFIG_READY);//只要不可变则final,不可变量达到最大化,优化程序;
        if (!isReady) {
            throw new RuntimeException("Configuration is not ready,call configure");
        }
    }*/


    @SuppressWarnings("unchecked")
    public final <T> T getConfiguration(Object key) {
       // checkConfiguration(); //先检查配置是否完成;
        final Object value = CONFIGS.get(key);
        if (value == null) {
            throw new NullPointerException(key.toString() + " IS NULL");
        }
        return (T) CONFIGS.get(key);
    }

    public  Context getApplicationContext() {
        return getConfiguration(ConfigKeys.APPLICATION_CONTEXT);
    }
}
